﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace iLangl.Model.Rules
{
	public class ValidateProject : IValidation
	{
		Project project;

		

		#region IValidation Members

		public IList<IError> Validate(object item)
		{
			List<IError> result = new List<IError>();

			if(!(item is Project))
			{
				result.Add(new Error(
					"Validation Failed", 
					String.Format("Expected type ='Project' instead of {0}", item.GetType()),
					eSeverity.High));

				return result;
			}

			project = item as Project;


			if (String.IsNullOrEmpty(project.Id))
			{
				result.Add(new Error(
				"Project Identity is not specified!",
				"Project.Id is required field!",
				eSeverity.Critical));
			}

			if (String.IsNullOrEmpty(project.Name))
			{
				result.Add(new Error(
				"Project Name is not specified!",
				"Project.Name is required field!",
				eSeverity.High));
			}

			if (project.Deadline.Ticks < DateTime.Now.Ticks)
			{
				result.Add(new Error(
				"Deadline time is less then current time!",
				"Wrong Deadline of project overdue!",
				eSeverity.Medium));
			}

			if (String.IsNullOrEmpty(project.Status))
			{
				result.Add(new Error(
				"Project Status is not specified!",
				"Project.Status is required field!",
				eSeverity.Medium));
			}

			if (project.WorkArea == null)
			{
				result.Add(new Error(
				"Project Terminology is not specified!",
				"Project.Terminology is required field!",
				eSeverity.Medium));
			}

			if (project.Product == null)
			{
				result.Add(new Error(
				"Project Product is not specified!",
				"Project.Product is required field!",
				eSeverity.Medium));
			}
			else
			{
				if (string.IsNullOrEmpty(project.Product.Id))
				{
					result.Add(new Error(
				"Project Product Identity is not specified!",
				"Project.Product.Id is required field!",
				eSeverity.Critical));
				}
			}

			if (project.Owner == null)
			{
				result.Add(new Error(
				"Project Owner is not specified!",
				"Project.Owner is required field!",
				eSeverity.Medium));
			}

			if (project.TechnicalContact == null)
			{
				result.Add(new Error(
				"Project TechnicalContact is not specified!",
				"Project.TechnicalContact is not specified!",
				eSeverity.Low));
			}
		

			if((project.Contents == null) || (project.Contents.Length <= 0))
			{
				result.Add(new Error(
				"Project Content for translation is empty!",
				"Project required content to translate!",
				eSeverity.Medium));
			}

			if ((project.Translations == null) || (project.Translations.Length <= 0))
			{
				result.Add(new Error(
				"Project should contain at least one translation!",
				"Project required at least one translation!",
				eSeverity.Medium));
			}

			if (project.Contents != null)
			{
				ValidateContent vcontent = new ValidateContent();
				//Validate Content
				foreach (ContentEntity ce in project.Contents)
				{
					result.AddRange(vcontent.Validate(ce));
				}
			}


			if (project.Translations != null)
			{
				ValidateTranslation vtranslation = new ValidateTranslation();
				//Validate Translation
				foreach (Translation trans in project.Translations)
				{
					result.AddRange(vtranslation.Validate(trans));
				}
			}

			//Validate History
			if ((project.History == null)||(project.History.Length <= 0))
			{
				result.Add(new Error(
				"Project should contain at least one history record!",
				"Project required at least one history record!",
				eSeverity.Medium));
			}
			else
			{
				ValidateStateHistory vhistory = new ValidateStateHistory();
				foreach (StateHistory shistory in project.History)
				{
					result.AddRange(vhistory.Validate(shistory));
				}
			}
			
			return result;
		}

		#endregion
	}
}
